from game.ai.action import Action
from game.fov import *

class CloseAction(Action):
    def __init(self):
        Action.__init__(self)
        #closed door, open it

    def perform(self, dat, src, dst):
        #source tile
        s_tile = dat.level.tiles[src[0]][src[1]]
        if s_tile is None or s_tile.occupant is None:
            return None

        #print src, dst
        nx = src[0] + dst[0]
        ny = src[1] + dst[1]

        #destination tile
        d_tile = dat.level.tiles[nx][ny]
        if d_tile.doodad is not None:
            dd = d_tile.doodad
            dmeth = getattr(self, 'perform__{0}_{1}'.format(dd.name, dd.state.name), None)
            if dmeth is not None:
                dmeth(dat, dd, s_tile, d_tile)

        return None


    def perform__door_closed(self, dat, doodad, s_tile, d_tile):
        dat.gui.msg_mgr.add_msg("You close the door tight. That'll teach it to already be closed. ")

    def perform__door_locked(self, dat, doodad, s_tile, d_tile):
        dat.gui.msg_mgr.add_msg("You close the locked door. Makes sense. ")

    def perform__door_open(self, dat, doodad, s_tile, d_tile):
        dat.res_mgr.load_doodad_state(doodad, 'closed')
        dat.gui.msg_mgr.add_msg("You grasp the knob and begin to close the door. It's hinges groan as the slam of wood on stone signifies the closing of the door. The thud is a resounding victory cry for all door-openers around the world, and possibly in other multiverses as well. ")
        dat.level.reblit_background(d_tile.x, d_tile.y)
        if s_tile.occupant == dat.main_char:
            dat.level.apply_fog()
            do_fov(dat.level, dat.main_char.pos(), 10)

